package dxn.nl.file.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dxn.nl.file.biz.dao.ExcelDealServerDao;
import dxn.nl.file.biz.po.FileDealServerPo;
import dxn.nl.file.biz.service.ExcelDealServerBaseService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ExcelDealServerBaseServiceImpl extends ServiceImpl<ExcelDealServerDao, FileDealServerPo> implements ExcelDealServerBaseService {

    @Override
    public int saveFileDealServer(FileDealServerPo po) {
        try{
            LambdaQueryWrapper<FileDealServerPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FileDealServerPo::getIp, po.getIp())
                    .eq(FileDealServerPo::getPort, po.getPort());

            FileDealServerPo appModelPo = this.baseMapper.selectOne(lambdaQueryWrapper);
            if(appModelPo == null)
            {
                this.baseMapper.insert(po);
            }

            this.baseMapper.update(po,lambdaQueryWrapper);
        }
        catch (Exception ex)
        {
            log.error("保存消费服务失败",ex);
        }
        return 0;
    }

    @Override
    public int removeDealServer(String sessionId) {
        try{
            LambdaQueryWrapper<FileDealServerPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FileDealServerPo::getSessionId, sessionId);

            return this.baseMapper.delete(lambdaQueryWrapper);
        }
        catch (Exception ex)
        {
            log.error("移除消费服务失败",ex);
        }
        return 0;
    }

    @Override
    public int refreshDealServer(String sessionId, LocalDateTime time) {
        try{
            LambdaQueryWrapper<FileDealServerPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FileDealServerPo::getSessionId, sessionId);

            FileDealServerPo data = new FileDealServerPo();
            data.setRefreshTime(LocalDateTime.now());

            return this.baseMapper.update(data,lambdaQueryWrapper);
        }
        catch (Exception ex)
        {
            log.error("保存注册消费服务失败",ex);
        }
        return 0;
    }
}
